﻿using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace PrismStudy.ViewModels
{
    class NavgationWindowViewModel : BindableBase
    {
        IRegionManager regionManager;
        private readonly IDialogService dialogService;

        /// <summary>
        /// 导航日志
        /// </summary>
        IRegionNavigationJournal regionNavigationJournal;



        /// <summary>
        /// 绑定属性，用于判断上一步下一步按钮是否可用。
        /// </summary>
        private bool canBack;
        private bool canForward;

        public bool CanBack { get { return canBack; } set { canBack = value;RaisePropertyChanged(); } }
        public bool CanForward { get { return canForward; } set { canForward = value;RaisePropertyChanged(); } }

        public DelegateCommand GoNextCommand { get; private set; }
        public DelegateCommand GoLastCommand { get; private set; }
        public DelegateCommand OpenACommand { get; private set; }
        public DelegateCommand OpenBCommand { get; private set; }

        public NavgationWindowViewModel(IRegionManager regionManager,IDialogService dialogService)
        {
            this.regionManager = regionManager;
            this.dialogService = dialogService;
            GoNextCommand = new DelegateCommand(GoNext);
            GoLastCommand = new DelegateCommand(GoLast);
            OpenACommand = new DelegateCommand(OpenA);
            OpenBCommand = new DelegateCommand(OpenB);
            CanBack = false;
            CanForward = false;
        }

        private void OpenB()
        {
            regionManager.RequestNavigate("NavgationRegion", "pageB",arg=> {
                regionNavigationJournal = arg.Context.NavigationService.Journal;
            });
            CanBack = regionNavigationJournal.CanGoBack;
            CanForward = regionNavigationJournal.CanGoForward;
        }

        private void OpenA()
        {
            DialogParameters param = new DialogParameters();
            param.Add("value","传递参数");
            dialogService.ShowDialog("MyDialog",param,arg => {
                if (arg.Result == ButtonResult.OK)
                {
                    string result = arg.Parameters.GetValue<string>("UserInput");
                    MessageBox.Show($"用户同意，输入的参数为：【{result}】");
                }
                else
                {
                    MessageBox.Show($"用户不同意");
                }
            });

            //在回调函数中写入导航日志
            regionManager.RequestNavigate("NavgationRegion", "pageA?param=Haaaaaa",arg => {
                regionNavigationJournal = arg.Context.NavigationService.Journal;
            });
            CanBack = regionNavigationJournal.CanGoBack;
            
        }

        private void GoLast()
        {
            regionNavigationJournal.GoBack();
            CanForward = regionNavigationJournal.CanGoForward;
            CanBack = regionNavigationJournal.CanGoBack;
        }

        private void GoNext()
        {
            regionNavigationJournal.GoForward();
            CanForward = regionNavigationJournal.CanGoForward;
            CanBack = regionNavigationJournal.CanGoBack;

        }
    }
}
